import 'package:flutter/material.dart';
import 'package:flutter_hsh/fluttergromore/ads/ad_feed_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import '../../style/style.dart';
import 'season_detail_logic.dart';

/// 季节详情页面
class SeasonDetailPage extends StatefulWidget {
  const SeasonDetailPage({Key? key}) : super(key: key);

  @override
  State<SeasonDetailPage> createState() => _SeasonDetailPageState();
}

class _SeasonDetailPageState extends State<SeasonDetailPage> {
  final SeasonDetailLogic logic = Get.find<SeasonDetailLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Obx(() {
        if (logic.isLoading.value) {
          return const Center(child: CircularProgressIndicator());
        }
        
        final seasonDetail = logic.seasonDetail.value;
        
        return Container(
          color: const Color(0xFFAFE3FF), // 浅蓝色背景
          child: SafeArea(
            child: Column(
              children: [
                // 顶部导航栏
                _buildAppBar(seasonDetail.name, seasonDetail.color),
                SizedBox(height: 10.h,),
                const MyAdFeedView(),
                SizedBox(height: 10.h,),
                // 内容区域
                Expanded(
                  child: SingleChildScrollView(
                    controller: logic.scrollController,
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 15.w),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          SizedBox(height: 20.h),
                          
                          // 季节介绍卡片
                          _buildSeasonIntroCard(seasonDetail),
                          
                          SizedBox(height: 20.h),
                          
                          // 节气列表
                          ...seasonDetail.jieqiList.map((jieqi) => 
                            _buildJieqiCard(jieqi, seasonDetail.color)
                          ).toList(),
                          
                          SizedBox(height: 30.h),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }),
    );
  }

  /// 构建顶部导航栏
  Widget _buildAppBar(String title, Color color) {
    return Container(
      height: 56.h,
      padding: EdgeInsets.symmetric(horizontal: 15.w),
      child: Row(
        children: [
          // 返回按钮
          GestureDetector(
            onTap: () => Get.back(),
            child: Container(
              width: 32.w,
              height: 32.w,
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(0.7),
                borderRadius: BorderRadius.circular(16.r),
              ),
              child: Icon(Icons.arrow_back_ios_new, size: 18.w, color: Colors.black87),
            ),
          ),
          
          SizedBox(width: 15.w),
          
          // 标题
          Text(
            title,
            style: TextStyle(
              fontSize: 20.sp,
              fontWeight: FontWeight.w500,
              color: color,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建季节介绍卡片
  Widget _buildSeasonIntroCard(SeasonDetail seasonDetail) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(15.r),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.7),
        borderRadius: BorderRadius.circular(15.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 标题和图标
          Row(
            children: [
              Container(
                width: 50.w,
                height: 50.w,
                decoration: BoxDecoration(
                  color: seasonDetail.color,
                  shape: BoxShape.circle,
                ),
                child: Center(
                  child: SvgPicture.asset(
                    seasonDetail.iconPath,
                    width: 30.w,
                    height: 30.w,
                  ),
                ),
              ),
              
              SizedBox(width: 15.w),
              
              Text(
                '${seasonDetail.name}简介',
                style: TextStyle(
                  fontSize: 18.sp,
                  fontWeight: FontWeight.w500,
                  color: Colors.black,
                ),
              ),
            ],
          ),
          
          SizedBox(height: 15.h),
          
          // 季节描述
          Text(
            seasonDetail.description,
            style: TextStyle(
              fontSize: 14.sp,
              color: Colors.black87,
              height: 1.5,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建节气卡片
  Widget _buildJieqiCard(JieqiInfo jieqi, Color color) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 15.h),
      padding: EdgeInsets.all(15.r),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.7),
        borderRadius: BorderRadius.circular(15.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 节气名称和日期
          Row(
            children: [
              Container(
                padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 5.h),
                decoration: BoxDecoration(
                  color: color,
                  borderRadius: BorderRadius.circular(15.r),
                ),
                child: Text(
                  jieqi.name,
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                    color: Colors.white,
                  ),
                ),
              ),
              
              SizedBox(width: 10.w),
              
              Text(
                jieqi.date,
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Colors.black54,
                ),
              ),
            ],
          ),
          
          SizedBox(height: 15.h),
          
          // 特点
          _buildJieqiInfoItem('特点：', jieqi.feature),
          
          SizedBox(height: 10.h),
          
          // 农事
          _buildJieqiInfoItem('农事：', jieqi.farming),
          
          SizedBox(height: 10.h),
          
          // 习俗
          _buildJieqiInfoItem('习俗：', jieqi.custom),
        ],
      ),
    );
  }

  /// 构建节气信息项
  Widget _buildJieqiInfoItem(String label, String content) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w500,
            color: Colors.black87,
          ),
        ),
        
        SizedBox(width: 5.w),
        
        Expanded(
          child: Text(
            content,
            style: TextStyle(
              fontSize: 14.sp,
              color: Colors.black87,
              height: 1.5,
            ),
          ),
        ),
      ],
    );
  }
}